﻿using System;
using System.Collections.Generic;
using System.Data;
using DataCenter.Interface;
using DataCenter.Share;

namespace DataCenter.Component.Adapter
{
    public class VerticalAdapter : IAdapter
    {
        public VerticalAdapter(VerticalAdapterEntity entity)
        {
            this._Entity = entity;
        }
        private VerticalAdapterEntity _Entity;
        private Dictionary<string, SortedList<DateTime, TimeLine>> DictLine;

        #region IDataAdapter Members

        public bool Execute(ISource source, string[] keyArray, DateTime time0, DateTime time1)
        {
            string keys = Utils.ArrayToString(keyArray);
            string sqlString = _Entity.SqlString;
            sqlString = sqlString.Replace("{@Keys}", keys);
            sqlString = sqlString.Replace("{@Time0}", time0.ToString());
            sqlString = sqlString.Replace("{@Time1}", time1.ToString());

            Logger.GetInstance().Debug("\t\t" + sqlString);

            DictLine = new Dictionary<string, SortedList<DateTime, TimeLine>>();
            using (IDataReader reader = source.GetReader(sqlString))
            {
                while (reader.Read())
                {
                    string key = Utils.GetReaderValue<string>(reader, _Entity.KeyField);
                    DateTime valueTime = Utils.GetReaderValue<DateTime>(reader, _Entity.TimeField);
                    float value = Utils.GetReaderValue<float>(reader, _Entity.ValueField);

                    SortedList<DateTime, TimeLine> dictDate = null;
                    if (DictLine.ContainsKey(key)) dictDate = DictLine[key];
                    else
                    {
                        dictDate = new SortedList<DateTime, TimeLine>();
                        DictLine.Add(key, dictDate);
                    }

                    TimeLine timeLine = null;
                    if (dictDate.ContainsKey(valueTime.Date))
                        timeLine = dictDate[valueTime.Date];
                    else
                    {
                        timeLine = new TimeLine();
                        timeLine.Name = key;
                        timeLine.Time = valueTime.Date;
                        dictDate.Add(valueTime.Date, timeLine);
                    }

                    TimePoint point = new TimePoint();
                    point.Time = valueTime;
                    point.Flag = Utils.REAL_FLAG;
                    point.Value0 = value;
                    timeLine.Add(point, TimeRepeatOperTypes.Ignore);
                }
            }

            return true;
        }

        public void GetValue(AdapterValueCallbackDelegate callback)
        {
            foreach (string key in DictLine.Keys)
            {
                foreach (DateTime valueTime in DictLine[key].Keys)
                {
                    if (!callback(key, valueTime, DictLine[key][valueTime]))
                        return;
                }
            }
        }

        #endregion

        #region IComponent Members

        public bool Initialize()
        {
            return true;
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            
        }

        #endregion
    }
}
